package com.kye.express.business.score;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.widget.RxCompoundButton;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemScoreTagBinding;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 19/01/2018
 * @updated : 19/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

class ScoreTagViewBinder extends ItemViewBinder<ScoreTagModel, ScoreTagViewBinder.ViewHolder> {

    public ScoreTagViewBinder() {
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_score_tag, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ScoreTagModel item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemScoreTagBinding binding;
        private ScoreTagModel item;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxCompoundButton
                    .checkedChanges(binding.scoreTag)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Boolean>() {
                        @Override
                        public void accept(Boolean aBoolean) throws Exception {
                            if (item != null) {
                                item.setSelected(aBoolean);
                            }
                        }
                    });
        }

        public void bindData(ScoreTagModel item) {
            this.item = item;
            binding.setData(item);
            binding.scoreTag.setEnabled(!item.isDisable());
        }
    }
}
